package com.mvetc.controller;

import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mvetc.entity.Manager;
import com.mvetc.service.ManagerService;
import com.mvetc.service.impl.ManagerServiceImpl;

/**
 * Servlet implementation class ManagerServlet
 */
@WebServlet("/ManagerServlet")
public class ManagerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private ManagerService managerService = new ManagerServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManagerServlet() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("login".equals(op)) {
			doLogin(request,response);
		}else if ("logout".equals(op)) {
			doLogout(request,response);
		}else if ("modifyManagerPassword".equals(op)) {
			doModifyManagerPassword(request,response);
		}
	}

	private void doModifyManagerPassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String managerId = request.getParameter("managerId");
		String oldManagerPassword = request.getParameter("oldManagerPassword");
		String newManagerPassword = request.getParameter("newManagerPassword");
		Map<String, String> managerMap = managerService.login(managerId, oldManagerPassword);
		if (managerMap != null) {
			Manager manager = new Manager(managerId, newManagerPassword);
			managerService.modifyPasswordById(manager);
			
			response.getWriter().write("密码修改成功");
		}else {
			response.getWriter().write("密码修改失败");
		}
	}

	private void doLogout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 清除session信息
		HttpSession session = request.getSession();
		session.removeAttribute("managerMap");
		// 重定向
		response.sendRedirect("back/html/login.jsp");
	}

	private void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String managerId = request.getParameter("managerId");
		String password = request.getParameter("password");
		Map<String, String> managerMap = managerService.login(managerId, password);
		
		if (managerMap != null) { 
			// 登录成功
			// 用户信息存储在session会话作用域中
			HttpSession session = request.getSession();
			session.setAttribute("managerMap", managerMap);
			
			// 获取复选框组件的值
			String rememberPassword = request.getParameter("rememberPassword");

			// 用户选择记住密码
			if(rememberPassword != null && "rememberPassword".equals(rememberPassword)) {
				// 通过 Servlet 添加 Cookie
				// 用户信息存储在cookie中,发送给HTTP响应头中
				Cookie cookie  = new Cookie(managerId, password);
				response.addCookie(cookie);
				
			}else {
				// 多用户操作,用户没有选择记住密码,删除现有的cookie
				Cookie[] cookies = request.getCookies();
				Cookie cookie = null;
				for (int i = 0; i < cookies.length; i++) {
					cookie = cookies[i];
					if(cookie.getName().equals(managerId)) { // 用户名就是cookie的name
						cookie.setMaxAge(0);
						response.addCookie(cookie);
					}
				}
			}
			
			//  重定向到首页
			response.getWriter().write("success");
		}else {
			// 登录失败 
			// 重定向到登录页面
			response.getWriter().write("failure");
		}
	}

}
